package com.college.controller.products;


import com.college.common.pojo.ao.Products;
import com.college.common.pojo.dto.products.AddProductsDTO;
import com.college.common.pojo.dto.products.QueryProductsDTO;
import com.college.common.pojo.dto.products.UpdateProductsDTO;
import com.college.common.pojo.model.page.PageParamRequest;
import com.college.common.response.CommonPage;
import com.college.common.response.CommonResult;
import com.college.common.utils.object.ObjectUtil;
import com.college.products.service.ProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品 前端控制器
 * @author 小C工作室
 * @since 2025-02-22
 */
@RestController
@RequestMapping("/products")
@Api(tags = "商品")
@Slf4j
public class ProductsController {

    @Autowired
    private ProductsService productsService;

    @ApiOperation(value = "查询商品列表")
    @GetMapping(value = "/query-list")
    public CommonResult<List<Products>> queryList(QueryProductsDTO queryProductsDTO) {
        return CommonResult.success(productsService.queryList(queryProductsDTO));
    }

    @ApiOperation(value = "查询商品列表分页")
    @GetMapping(value = "/query-list-page")
    public CommonResult<CommonPage<Products>> queryListPage(QueryProductsDTO queryProductsDTO , PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(productsService.queryListPage(queryProductsDTO,pageParamRequest)));
    }

    @ApiOperation(value = "新增商品")
    @PostMapping(value = "/add")
    public CommonResult<String> addProducts(@RequestBody AddProductsDTO addProductsDTO) {
        return productsService.addProducts(addProductsDTO);
    }

    @ApiOperation(value = "删除商品")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResult<Boolean> deleteProducts(@PathVariable Long id) {
        return CommonResult.success(productsService.removeById(id));
    }

    @ApiOperation(value = "批量删除商品")
    @DeleteMapping(value = "/batch-delete")
    public CommonResult<Boolean> deleteProducts(@RequestBody List<Long> ids) {
        if (ObjectUtil.isNotEmpty(ids)) {
            return CommonResult.success(productsService.removeByIds(ids));
        }
        return CommonResult.success("参数错误");
    }

    @ApiOperation(value = "更新商品")
    @PutMapping(value = "/update")
    public CommonResult<String> updateProducts(@RequestBody UpdateProductsDTO updateProductsDTO) {
        return productsService.updateProducts(updateProductsDTO);
    }
}

